//
//  CZSQLiteManager.swift
//  iOSWeibo10
//
//  Created by itcast on 16/5/26.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import FMDB

class CZSQLiteManager: NSObject {

    static let sharedManager = CZSQLiteManager()
    
    // 数据库
    let dbQueue: FMDatabaseQueue
    
    private override init() {
        let dbPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! + "/status.db"
        
        dbQueue = FMDatabaseQueue(path: dbPath)
        super.init()
        createTable("T_Status")
    }
    
    private func createTable(tableName: String) {
        let sql = "CREATE TABLE IF NOT EXISTS \(tableName) (statusId INTEGER PRIMARY KEY, status TEXT, userId INTEGER, createTime TEXT DEFAULT (datetime('now', 'localtime')));"
        
        dbQueue.inDatabase { (db) -> Void in
            if db.executeUpdate(sql, withArgumentsInArray: nil) {
                print("创建微博表成功")
            } else {
                print("创建微博表失败")
            }
        }
    }
}
